package com.haotunet.app.goldtreasure.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.haotunet.app.goldtreasure.R;
import com.haotunet.app.goldtreasure.adapter.MediaAdapter;
import com.haotunet.app.goldtreasure.model.BaseModel;
import com.haotunet.app.goldtreasure.model.MediaInfo;
import com.haotunet.app.goldtreasure.model.MediaModel;
import com.haotunet.app.goldtreasure.net.ActionUtil;
import com.haotunet.app.goldtreasure.other.Interface.OnPassBackListener;
import com.haotunet.app.goldtreasure.utils.UIHelper;
import com.haotunet.app.pulltorefresh.PullToRefreshLayout;
import com.haotunet.app.pulltorefresh.PullToRefreshLayout.OnRefreshListener;
import com.haotunet.app.pulltorefresh.pullableView.PullableListView;

import java.util.List;

public class MediaFragment extends BaseFragment {
	private PullableListView lvMedia;
	private PullToRefreshLayout mfreshLayout;
	private boolean isPrepared;
	private boolean mHasLoadedOnce;
	private View view;
	private int pageNum = 20;
	private MediaAdapter mediaAdapter;
	private final int GET_MEDIA_INFO_SUCCESS = 2;
	private final int LOAD_MORE_MEDIA_INFO_SUCCESS = 3;

	private Handler handle = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {

				case GET_MEDIA_INFO_SUCCESS:
					MediaModel mediaModel = (MediaModel) msg.obj;
					List<MediaInfo> mediaList = mediaModel.getArticleList();
					mediaAdapter = new MediaAdapter(getActivity(), mediaList);
					lvMedia.setAdapter(mediaAdapter);

					break;
				case LOAD_MORE_MEDIA_INFO_SUCCESS:
					MediaModel moreModel = (MediaModel) msg.obj;
					if (moreModel.getArticleList().size() == 0) {
						UIHelper.showToast(getActivity(), getString(R.string.last_page_hint));
					}
					mediaAdapter.addDatas(moreModel.getArticleList());
					break;
				default:
					break;
			}
		}

	};
	private int type;

	public static MediaFragment newInstance(int type) {
		Bundle args = new Bundle();
		args.putInt("type", type);
		MediaFragment fragment = new MediaFragment();
		fragment.setArguments(args);
		return fragment;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.fragment_media, null);
		initView();
		isPrepared = true;
		type = getArguments().getInt("type");
	/*	if (type==2) {
			getDiscoverNews(type, "0", "");
		}*/
		return view;
	}

	private void initView() {
		mfreshLayout = (PullToRefreshLayout) view.findViewById(R.id.id_swipe_ly);
		lvMedia = (PullableListView) view.findViewById(R.id.lv_media);
		RefreshData(0);
	}

	private void RefreshData(final int pagenum) {
		mfreshLayout.setOnRefreshListener(new OnRefreshListener() {

			@Override
			public void onRefresh(final PullToRefreshLayout pullToRefreshLayout) {
				getDiscoverNews(type, pagenum + "", "refresh");
				pageNum = 20;
			}

			@Override
			public void onLoadMore(final PullToRefreshLayout pullToRefreshLayout) {
				getDiscoverNews(type, pageNum + "", "loadmore");
			}
		});
	}

	public void getDiscoverNews(int type, String pagenum, final String action) {
		ActionUtil actionUtil = new ActionUtil(getActivity());
		actionUtil.getDiscoverNews(type, pagenum);
		actionUtil.setOnPassBackListener(new OnPassBackListener() {

			@Override
			public void passBack(BaseModel model) {
				if (!isActivityAvailable()) {
					return;
				}
				MediaModel mediaModel = (MediaModel) model;
				if (mediaModel == null) {
					UIHelper.showToast(getActivity(), getString(R.string.rank_fail));
					return;
				}
				if (mediaModel.getErrorMsg() != null) {
					UIHelper.showToast(getActivity(), mediaModel.getErrorMsg());
				} else {
					mHasLoadedOnce = true;
					if (action.equals("refresh") || action.equals("loadmore")) {
						mfreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED);
					}
					Message msg = new Message();
					if (action.equals("loadmore")) {
						msg.what = LOAD_MORE_MEDIA_INFO_SUCCESS;
						pageNum = pageNum + 20;
					} else {
						msg.what = GET_MEDIA_INFO_SUCCESS;
					}
					msg.obj = mediaModel;
					handle.sendMessage(msg);
				}
			}
		});
	}

	@Override
	protected void lazyLoad() {
		if (!isPrepared || !isVisible || mHasLoadedOnce) {
			return;
		}
		getDiscoverNews(type, "0", "");
	}

}
